// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Serialization;

namespace Microsoft.Internal
{
    internal static class Assumes
    {
        [DebuggerStepThrough]
        internal static void NotNull(object value)
        {
            IsTrue(value != null);
        }

        [DebuggerStepThrough]
        internal static void NotNull(object value1, object value2)
        {
            NotNull(value1);
            NotNull(value2);
        }

        [DebuggerStepThrough]
        internal static void NotNull(object value1, object value2, object value3)
        {
            NotNull(value1);
            NotNull(value2);
            NotNull(value3);
        }

        [DebuggerStepThrough]
        internal static void NotNullOrEmpty(string value)
        {
            NotNull(value);
            IsTrue(value.Length > 0);
        }

        [DebuggerStepThrough]
        internal static void Null(object value)
        {
            IsTrue(value == null);
        }

        [DebuggerStepThrough]
        internal static void IsFalse(bool condition)
        {
            if (condition)
            {
                Fail(null);
            }
        }

        [DebuggerStepThrough]
        internal static void IsTrue(bool condition)
        {
            if (!condition)
            {
                Fail(null);
            }
        }

        [DebuggerStepThrough]
        internal static void IsTrue(bool condition, string message)
        {
            if (!condition)
            {
                Fail(message);
            }
        }

        [DebuggerStepThrough]
        internal static void Fail(string message)
        {
            FailFast(message);
        }

        private static void FailFast(string message)
        {
            // Need to use Debug.Assert instead of Debug.Fail because Silverlight doesn't contain Debug.Fail.
            Debug.Assert(false, "Assumes failure: " + message);
            throw new InternalErrorException(message);
        }

        [Serializable]
        [SuppressMessage("Microsoft.Design", "CA1064:ExceptionsShouldBePublic")]
        private class InternalErrorException : Exception
        {
            public InternalErrorException(string message)
                : base(string.Format(CultureInfo.CurrentCulture, Strings.InternalExceptionMessage, message))
            {
            }

#if !SILVERLIGHT
            protected InternalErrorException(SerializationInfo info, StreamingContext context)
                : base(info, context)
            {
            }
#endif
        }
    }
}
